// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_dao

import (
	"context"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type BasicRoadmapInfoDbItem struct {
	Name            string `bson:"name"`
	Desc            string `bson:"desc"`
	EnableJoinApply bool   `bson:"enableJoinApply"`
}

type RoadmapInfoDbItem struct {
	RoadmapId   string                 `bson:"_id"`
	BasicInfo   BasicRoadmapInfoDbItem `bson:"basicInfo"`
	OwnerUserId string                 `bson:"ownerUserId"`
	CreateTime  int64                  `bson:"createTime"`
	UpdateTime  int64                  `bson:"updateTime"`
}

func (item *RoadmapInfoDbItem) ToRoadmapInfo() *roadmap_api.RoadmapInfo {
	return &roadmap_api.RoadmapInfo{
		RoadmapId: item.RoadmapId,
		BasicInfo: &roadmap_api.BasicRoadmapInfo{
			Name:            item.BasicInfo.Name,
			Desc:            item.BasicInfo.Desc,
			EnableJoinApply: item.BasicInfo.EnableJoinApply,
		},
		OwnerUserId: item.OwnerUserId,
		CreateTime:  item.CreateTime,
		UpdateTime:  item.UpdateTime,
	}
}

type _RoadmapInfoDao struct {
	mc *mongo.Collection
}

func newRoadmapInfoDao(db *mongo.Database) (*_RoadmapInfoDao, error) {
	mc := db.Collection(common.DB_NS_ROADMAP + "info")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"ownerUserId", 1}, {"createTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_RoadmapInfoDao{mc: mc}, nil
}

func (db *_RoadmapInfoDao) Insert(ctx context.Context, dbItem *RoadmapInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_RoadmapInfoDao) Exist(ctx context.Context, roadmapId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": roadmapId})
	return count > 0, err
}

func (db *_RoadmapInfoDao) Get(ctx context.Context, roadmapId string) (*RoadmapInfoDbItem, error) {
	retItem := &RoadmapInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": roadmapId}).Decode(retItem)
	return retItem, err
}

func (db *_RoadmapInfoDao) Remove(ctx context.Context, roadmapId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": roadmapId})
	return err
}

func (db *_RoadmapInfoDao) ListByOwner(ctx context.Context, ownerUserId string) ([]*RoadmapInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"ownerUserId": ownerUserId}, options.Find().SetSort(bson.D{{"createTime", -1}}))
	if err != nil {
		return nil, err
	}
	retList := []*RoadmapInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RoadmapInfoDao) ListById(ctx context.Context, roadmapIdList []string) ([]*RoadmapInfoDbItem, error) {
	if len(roadmapIdList) == 0 {
		return []*RoadmapInfoDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": roadmapIdList}}, options.Find().SetSort(bson.D{{"createTime", -1}}))
	if err != nil {
		return nil, err
	}
	retList := []*RoadmapInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RoadmapInfoDao) ListByApply(ctx context.Context, userId string) ([]*RoadmapInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"ownerUserId": userId, "basicInfo.enableJoinApply": true}, options.Find().SetSort(bson.D{{"createTime", -1}}))
	if err != nil {
		return nil, err
	}
	retList := []*RoadmapInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RoadmapInfoDao) UpdateBasic(ctx context.Context, roadmapId string, basicItem *BasicRoadmapInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": roadmapId}, bson.M{
		"$set": bson.M{
			"basicInfo":  basicItem,
			"updateTime": updateTime,
		},
	})
	return err
}
